package com.tyqx.hashchainbiz.service.impl;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import com.tyqx.hashchainbean.module.*;
import com.tyqx.hashchainbean.test.BaiWanTest;
import com.tyqx.hashchainbean.vo.MongoDbInfoNoListVO;
import com.tyqx.hashchainbean.vo.MongoDbInfoVO;
import com.tyqx.hashchainbiz.service.MysqlDataBaseAndMongDbConfigService;
import com.tyqx.hashchainbiz.service.WxAccountFirmVerificationRelationService;
import com.tyqx.hashchainbiz.service.WxAccountFirmVerificationService;
import com.tyqx.hashchaindao.mapper.*;
import com.tyqx.hashchainutil.common.mongodb.MongoDBUtil;
import hashchainutil.common.EmptyUtils;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service("mysqlDataBaseAndMongDbConfigService")
public class MysqlDataBaseAndMongDbConfigServiceImpl implements MysqlDataBaseAndMongDbConfigService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private JZTLogisticsMapper jztLogisticsMapper;

    @Resource
    private JZTAttachmentBusinessMapper jztAttachmentBusinessMapper;

    @Resource
    private NoHashValueJsonMapper noHashValueJsonMapper;

    @Resource
    private HashChainStatisticsMapper hashChainStatisticsMapper;

    @Resource
    private JZTCodeCirculationMapper jztCodeCirculationMapper;

    @Resource
    private JZTCodeCirculationAttachmentMapper jztCodeCirculationAttachmentMapper;

    @Resource
    private JZTGS1Mapper jztgs1Mapper;

    @Resource
    private JZTGS1AttachmentMapper jztgs1AttachmentMapper;

    @Resource
    private BaiWanTestMapper baiWanTestMapper;

    @Resource
    private WxAccountFirmVerificationMapper wxAccountFirmVerificationMapper;


    @Resource
    private WxAccountFirmVerificatioRelationMapper wxAccountFirmVerificatioRelationMapper;
    @Resource
    private MongoDBUtil mongoDBUtil;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer recordJZTLogistics(String hashValue, HashIndex hashIndex, JZTLogistics jztLogistics) throws Exception {
        Integer flag1 = hashIndexMapper.insert(hashIndex);
        Integer flag2 = jztLogisticsMapper.addJZTLogistics(jztLogistics);
        Integer flag3 = noHashValueJsonMapper.deleteByHashValue(hashValue);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer recordJZTLogisticsBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTLogistics> jztLogisticsList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztLogisticsMapper.insertBatch(jztLogisticsList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer recordJZTAttachmentBusiness(String hashValue, HashIndex hashIndex, JZTAttachmentBusiness jztAttachmentBusiness) throws Exception {
        Integer flag1 = hashIndexMapper.insert(hashIndex);
        Integer flag2 = jztAttachmentBusinessMapper.addJZTAttachmentBusiness(jztAttachmentBusiness);
        Integer flag3 = noHashValueJsonMapper.deleteByHashValue(hashValue);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer recordJZTAttachmentBusinessBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTAttachmentBusiness> jztAttachmentBusinessList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztAttachmentBusinessMapper.insertBatch(jztAttachmentBusinessList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ObjectId insertDataCountAndMongoDBGridFS(HashChainStatistics hashChainStatistics, String fileName, InputStream inputStream, GridFSUploadOptions options, GridFSBucket gridFSFilesBucket) throws Exception {
        Integer flag = hashChainStatisticsMapper.add(hashChainStatistics);
        if (flag == 0) {
            throw new Exception("record statistcs error");
        }
        ObjectId fileId = gridFSFilesBucket.uploadFromStream(fileName, inputStream, options);

        return fileId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertDataCountAndMongoDB(HashChainStatistics hashChainStatistics, String collName, MongoDbInfoVO mongoDbInfoVO) throws Exception {
        Integer flag = hashChainStatisticsMapper.add(hashChainStatistics);
        if (flag == 0) {
            throw new Exception("record statistcs error");
        }
        mongoDBUtil.insertOne(collName, mongoDbInfoVO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertDataCountAndMongoDBSingle(HashChainStatistics hashChainStatistics, String collName, MongoDbInfoNoListVO mongoDbInfoNoListVO) throws Exception {
        Integer flag = hashChainStatisticsMapper.add(hashChainStatistics);
        if (flag == 0) {
            throw new Exception("record statistcs error");
        }
        mongoDBUtil.insertOne(collName, mongoDbInfoNoListVO);
        return true;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertDataMongoDB(String collName, MongoDbInfoVO mongoDbInfoVO) throws Exception {
        mongoDBUtil.insertOne(collName, mongoDbInfoVO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer recordJZTCodeCirculationBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTCodeCirculation> jztCodeCirculationList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztCodeCirculationMapper.insertBatch(jztCodeCirculationList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer recordJZTCodeCirculationAttachmentBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTCodeCirculationAttachment> jztCodeCirculationAttachmentList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztCodeCirculationAttachmentMapper.insertBatch(jztCodeCirculationAttachmentList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer recordJZTGS1Batch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTGS1Str> jztgs1List) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztgs1Mapper.insertBatch(jztgs1List);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer recordJZTGS1AttachmentBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTGS1Attachment> jztgs1AttachmentList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztgs1AttachmentMapper.insertBatch(jztgs1AttachmentList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer recordBaiWanTestBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<BaiWanTest> baiWanTestList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = baiWanTestMapper.insertBatch(baiWanTestList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer recordWxAccountFirmVerificationBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<WxAccountFirmVerification> wxAccountFirmVerificationList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = wxAccountFirmVerificationMapper.insertBatch(wxAccountFirmVerificationList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addVerificationAndRelation(WxAccountFirmVerification wxAccountFirmVerification, WxAccountFirmVerificationRelation wxAccountFirmVerificationRelation) throws Exception {
        Integer flag1 = wxAccountFirmVerificationMapper.insertReturnId(wxAccountFirmVerification);
        if(EmptyUtils.isNotEmpty(wxAccountFirmVerification.getId()) && flag1 != 0){
            wxAccountFirmVerificationRelation.setVerificationId(wxAccountFirmVerification.getId());
            Integer flag = wxAccountFirmVerificatioRelationMapper.insert(wxAccountFirmVerificationRelation);
            if (flag == 0) {
                throw new Exception("record error");
            }
        }else{
            throw new Exception("record error");
        }
        return 1;
    }
}
